'''
功能层：封装业务流程；
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')
from web_base.base_page import BasePage


class BaiduPage(BasePage):
    url = r'https://www.baidu.com/'
    # 搜索定位
    kw_loc = (By.ID, 'kw')
    su_loc = (By.ID, 'su')

    # 登录定位
    login_btn_loc = (By.XPATH, '//*[@id="s-top-loginbtn"]')
    username_loc = (By.ID, 'TANGRAM__PSP_11__userName')
    password_loc = (By.ID, 'TANGRAM__PSP_11__password')
    isagree_loc = (By.ID, 'TANGRAM__PSP_11__isAgree')
    login_submit_loc = (By.ID, 'TANGRAM__PSP_11__submit')

    #   hao123定位
    hao_loc = (By.XPATH, '//*[@id="s-top-left"]/a[2]')



    def __init__(self, driver):
        super().__init__(driver)  # 使用 super() 调用父类的构造方法
        self.get_url(self.url)

    def search(self, kw):
        self.ele_clear(self.kw_loc)
        self.input_ele(self.kw_loc, kw)
        self.ele_click(self.su_loc)
        sleep(3)

    def login(self, username, password):
        self.ele_click(self.login_btn_loc)
        sleep(3)
        self.ele_clear(self.username_loc)
        self.input_ele(self.username_loc, username)
        self.ele_clear(self.password_loc)
        self.input_ele(self.password_loc, password)
        self.ele_click(self.isagree_loc)
        self.ele_click(self.login_submit_loc)
        sleep(3)

    def hao123(self):

        self.ele_click(self.hao_loc)
        sleep(3)



# 测试代码
if __name__ == '__main__':
    kw = 'selenium'
    username = '123'
    password = '456'

    chrome_location = r'D:\RJCS\谷歌浏览器内网通紫域和驱动安装\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)
    baidu_page = BaiduPage(driver)

    # # 测试搜索
    baidu_page.search(kw)

    # 测试登录
    # baidu_page.assert(username, password)

    # 测试hao123
    #baidu_page.hao123()

    driver.quit()
